home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Workspace / Briefcase / Source / Briefcase.m < prev    next >
Text File  |  1992-08-10  |  6KB  |  220 lines

  1. #import "Briefcase.h"
  2. #import "BriefDoc.h"
  3. #import "PrefDelegate.h"
  4. #import <appkit/appkit.h>
  5.  
  6. @implementation Briefcase:MultApp
  7.  
  8. /* Canon Information Systems is not responsible for anything anyone does with this   */
  9. /* code, nor are they responsible for the correctness of this code.  Basically, this */
  10. /* has very little to do with the company I work for, and you can't blame them.      */
  11.  
  12. /* This file is best read in a window as wide as this comment, and with tab settings */
  13. /* of 4 spaces and indent setting of 4 spaces (at least, that's what I use).         */
  14.  
  15. /* You are welcome to do as you would with this file under the following conditions. */
  16. /* First, I accept no blame for anything that goes wrong no matter how you use it,   */
  17. /* no matter how catastrophic, not even if it stems from a bug in my code.             */
  18. /* Second, please keep my notices on it when/if you distribute it.                     */
  19. /* Third, if you discover any bugs or have any comments, PLEASE TELL ME!  Code won't */
  20. /* get better without people picking it apart and giving the writer feedback.        */
  21. /* Fourth, if you modify it, please keep a notice that your version is based on mine */
  22. /* in the source files (and keep the notice that mine is based on four other pieces  */
  23. /* of code :<).  Thanks, and have fun.  - Subrata Sircar, ssircar@canon.com             */
  24.  
  25. /* This class is an instance of the application delegate class.        */
  26. /* It handles the drag and drop functions, and the communication    */
  27. /* with the Workspace.                                                */
  28.  
  29. /* Version 1.0b        Apr-19-92        Third Public Release    */
  30.  
  31. #define MY_VERSION        100
  32.  
  33. /* Public Methods */
  34.  
  35. - (int)delay
  36. {
  37.     return timeDelay;
  38. }
  39.  
  40. - setDelay:(int)value;
  41. {
  42.     timeDelay = value;
  43.     return self;
  44. }
  45.  
  46.  
  47. /* Target/Action Methods */
  48.  
  49. - addFile:sender
  50. /*
  51.  * Called by pressing Add File... in the Document menu.
  52.  */
  53. {
  54.     char tempBuf[8192];
  55.     char fileBuf[MAXPATHLEN+1];
  56.     const char *directory;
  57.     const char *const *files;
  58.     static const char *const myType[1] = {NULL};
  59.     id openpanel = [[OpenPanel new] allowMultipleFiles:YES];
  60.  
  61.     sprintf(tempBuf,"");
  62.     directory = [[self currentDocument] directory];
  63.     if (directory && (*directory == '/')) [openpanel setDirectory:directory];
  64.     if ([openpanel runModalForTypes:myType]) {
  65.         files = [openpanel filenames];
  66.         directory = [openpanel directory];
  67.         while (files && *files) {
  68.     /* Get pathnames and add them to the current Document */
  69.             sprintf(fileBuf,"%s/%s\n",directory,*files);
  70.             strcat(tempBuf,fileBuf);
  71.             files++;
  72.         }
  73.         [[self currentDocument] addFiles:tempBuf];
  74.     }
  75.  
  76.     return self;
  77. }
  78.  
  79.  
  80. /* Automatic update methods */
  81.  
  82. - (BOOL)validateCommand:menuCell
  83. {
  84.     if ([super validateCommand:menuCell]) {
  85.         if ([menuCell action] == @selector(addFile:)) return [self currentDocument] ? YES : NO;
  86.         else return YES;
  87.     } else return NO;
  88. }
  89.  
  90.  
  91. /* Application Delegate Methods */
  92.  
  93. - appWillInit:sender
  94. {
  95.     char temp[MAXPATHLEN+1];
  96.     /* check and load appropriate defaults */
  97.     
  98.     [super appWillInit:self];
  99.     [[self class] setDocClass:[BriefDoc class]];
  100.     [[self class] setVersion:MY_VERSION];
  101.     
  102.     if (!NXGetDefaultValue([NXApp appName],LocalString("TimeDelay"))) sprintf(temp,"1000");
  103.     else sprintf(temp,NXGetDefaultValue([NXApp appName],LocalString("TimeDelay")));
  104.     sscanf(temp,"%d",&timeDelay);
  105.  
  106.     return self;
  107. }
  108.  
  109. - appWillTerminate:sender
  110. /*
  111.  * Overridden to be sure all documents get an opportunity to be saved
  112.  * before exiting the program.  Save the defaults too.
  113.  */
  114. {
  115.     char temp[100];
  116.  
  117.     sprintf(temp,"%d",timeDelay);
  118.     NXWriteDefault([NXApp appName],LocalString("TimeDelay"),temp);
  119.     return [super appWillTerminate:self];
  120. }
  121.     
  122.  
  123. /* WARNING!  Hack alert!!  */
  124. /* When an application is inactive, the main window has no meaning.  Apparently it */
  125. /* also doesn't become active until after the appDidBecomeActive: message is sent. */
  126. /* Hence, I queue up a delayed message such that timeDelay milliseconds after the  */
  127. /* application activates, it updates its main window and adds the files then.      */
  128.  
  129. static char myBuf[MAX_STRING_ARRAY];
  130.  
  131. - pleaseUpdate:sender
  132. {
  133.     /* Update the drag/drop files and clear the buffer */
  134.     [[self currentDocument] addFiles:myBuf];
  135.     *myBuf = '\0';
  136.     return self;
  137. }
  138.     
  139. - appDidBecomeActive:sender
  140. {
  141.     return [self perform:@selector(pleaseUpdate:) with:self afterDelay:timeDelay cancelPrevious:YES];
  142. }
  143.  
  144.  
  145. /* Listener/Speaker remote methods */
  146.  
  147. // IconDragging Stuff, oh boy, oh boy!
  148. static char **filePath = NULL;
  149. static int files = 0;
  150.  
  151. - (int)iconEntered:(int)windowNum at:(double)x :(double)y
  152.     iconWindow:(int)iconWindowNum iconX:(double)iconX iconY:(double)iconY
  153.     iconWidth:(double)iconWidth iconHeight:(double)iconHeight
  154.     pathList:(const char *)pathList
  155. {
  156.     char *stringPos, *tempPtr;
  157.     char tempBuf[MAXPATHLEN+1];
  158.     int count = 0;
  159.     
  160.     if (filePath) {
  161.         freeList(filePath);
  162.         filePath = NULL;
  163.     }
  164.     stringPos = tempPtr = (char *)pathList;
  165.     files = 0;
  166.         
  167.     /* This code just parses the pathList for the filenames and explictly */
  168.     /* null-terminates them after copying into a buffer.  It appends that */
  169.     /* buffer to the dynamically-allocated filelist which keeps track of it */
  170.  
  171.     /* the number of tabs + 1 equals the number of files dragged in */
  172.     while (stringPos = index(stringPos, '\t')) {
  173.         count = (int)(stringPos-tempPtr);
  174.         strncpy(tempBuf,tempPtr,count);
  175.         *(tempBuf+count) = '\0';
  176.         filePath = addFile(tempBuf,filePath,files,MyZone);
  177.         files++;
  178.         stringPos++;
  179.         tempPtr=stringPos;
  180.     }
  181.     strncpy(tempBuf,tempPtr,strlen(tempPtr));
  182.     *(tempBuf+strlen(tempPtr)) = '\0';
  183.     filePath = addFile(tempBuf,filePath,files,MyZone);
  184.     files++;
  185.  
  186.     return 0;
  187. }
  188.  
  189. - (int)iconExitedAt:(double)x :(double)y
  190. // Erase those files someone waved through our icon
  191. {
  192.     freeList(filePath);
  193.     filePath = NULL;
  194.     files = 0;
  195.     return 0;
  196. }
  197.  
  198. - (int)iconReleasedAt:(double)x :(double)y ok:(int *)flag
  199. // Add the fileList to the buffer which will be added upon activation
  200. {
  201.     char tempBuf[MAXPATHLEN+1];
  202.     
  203.     /* accept the icon */
  204.     *flag = 1;
  205.    
  206.     while (files--) {
  207.         if (*(filePath[files])) {
  208.             sprintf(tempBuf,"%s\n",filePath[files]);
  209.             strcat(myBuf,tempBuf);
  210.         } else {
  211.             strcat(myBuf,"/\n");    // Someone waved the hard disk in
  212.         }        
  213.     }
  214.     
  215.     return 0;
  216. }
  217.  
  218. @end
  219.  
  220.